from turtle import numinput


class Node:
    def __init__(self,item):
        self.item = item
        self.next = None

    def __repr__(self):
        return str(self.item)


class linklist:
    def __init__(self):
        self.head = None
        self.tail = None
        self.num = 0
        
        
    def Hcrweate_linklist(self,li:list):
        head = Node(li[0])
        head.next = self.head
        num = 1
        for element in li[1:]:
            node = Node(element)
            node.next = head
            head = node
            num += 1
        self.head = head
        self.num += num
    
    def Tcrweate_linklist(self,li:list):
        head = Node(li[0])
        tail = head
        num = 1
        for element in li[1:]:
            node = Node(element)
            tail.next = node
            tail = node
            num += 1
        self.tail.next = head
        self.num += num
    
    def print_linklist(self):# 循环输出节点
        li = self.head
        while li:
            print(li.item, end = " ")
            li = li.next
        print()

    def insert_Node(self,index,node): #插入节点
        if index: #如果索引不为0
            li = self.head
            for i in range(1,index):
                li = li.next
            node.next = li.next
            li.next = node
        else:
            node.next = self.head
            self.head = node

    def delete_Node(self,index): # 删去节点
        if index > 1:
            li = self.head
            for i in range(1,index):
                li = li.next
            li.next = li.next.next
        elif index == 1:
            self.head.next = self.head.next.next
        else:
            self.head = self.head.next

    def search_Index(self,index): # 索引节点
        if index:
            point = self.head
            for x in range(0,index):
                point = point.next
            return point
        return self.head

    def search_Node(self,element):# 索引位置
        point = self.head
        num = 0
        while(point.item!= element ):
            point = point.next
            num += 1
        return num
        



if __name__ == "__main__":
    N1 = linklist()
    l1 = [1,2,3,4]
    N1.Hcrweate_linklist(l1)
    N1.print_linklist()
    N1.insert_Node(1,Node(7))
    N1.print_linklist()
    N1.delete_Node(1)
    N1.print_linklist()
    print(N1.search_Node())